package com.jiuli.liping.controller;

import com.jiuli.liping.service.OnDutyHisService;
import com.jiuli.liping.util.DateUtils;
import com.jiuli.liping.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Author zyl
 * Date  2020-03-25
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/ondutyhis")
public class OnDutyHisController {
    @Autowired
    private OnDutyHisService onDutyHisService;

    /**
     * 查询当天用户值班数据
     * @param
     * @return
     */
    @RequestMapping(value = {"/getOnDutyHis"}, method = RequestMethod.GET)
    public OnDutyHis getOnDutyHis(@RequestParam String code) {
        OnDutyHis onDutyHis = onDutyHisService.getOnDutyHis(code);
        return onDutyHis;
    }

    /**
     * 更新上传值班记录
     * @param
     * @return
     */
    @RequestMapping(value = "/updateOnDutyHis", method = RequestMethod.POST)
    public String updateOnDutyHis(@RequestBody OnDutyHis onDutyHis) {
        onDutyHis.setStatus("3");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 22:00:00");//设置日期格式
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        System.out.println(df2.format(new Date()));// new Date()为获取当前系统时间
        if(DateUtils.compare(df2.format(new Date()),df.format(new Date()))==true){
            onDutyHis.setStatus("4");
        }
        if(onDutyHis.getFlag()==false){
            onDutyHis.setStatus("5");
        }
        if(DateUtils.compare(df2.format(new Date()),df.format(new Date()))==true && onDutyHis.getFlag()==false){
            onDutyHis.setStatus("6");
        }
        onDutyHis.setCompleteTime(df2.format(new Date()));
        if (onDutyHisService.updateOnDutyHis(onDutyHis) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    /**
     * 更新上传值班记录
     * @param
     * @return
     */
    @RequestMapping(value = "/updateTrue", method = RequestMethod.POST)
    public String updateTrue(@RequestBody OnDutyHis onDutyHis) {
        OnDutyHis his = onDutyHisService.selectOdh(onDutyHis);
        if(his!=null){
            return "yqr";
        }
        if (onDutyHisService.updateTrue(onDutyHis) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @PostMapping("/upload")
    @ResponseBody
    public List<String> fileUpload(@RequestParam("file") MultipartFile[] files){
        List<String> list = new ArrayList<>();
        if (Objects.isNull(files)){
            return null;
        }
        for (int i = 0; i < files.length; i++) {
            String url = onDutyHisService.upload(files[i]);
            list.add(url);
        }
        return list;
    }

    @RequestMapping(value = {"/deleteImg"}, method = RequestMethod.POST)
    public Message deleteImg(@RequestParam String imgName){
        return onDutyHisService.deleteImg(imgName);
    }

    /**
     * 定位
     * @param
     * @return
     */
    @RequestMapping(value = {"/getLocation"}, method = RequestMethod.POST)
    public Location getLocation(@RequestBody Location location) {
        Location location2 = onDutyHisService.getLocation(location);
        return location2;
    }

    /**
     * 定位地址
     * @param
     * @return
     */
    @RequestMapping(value = {"/getAddress"}, method = RequestMethod.GET)
    public Address getAddress(@RequestParam String location) {
        Address address = onDutyHisService.getAddress(location);
        return address;
    }

    /**
     * 当月未值班信息
     * @param
     * @return
     */
    @RequestMapping(value = {"/getUserMonth"}, method = RequestMethod.GET)
    public OnDutyHis getUserMonth(@RequestParam String code) {
        OnDutyHis onDutyHis = onDutyHisService.getUserMonth(code);
        return onDutyHis;
    }

    /**
     * 获取可调班人员信息
     * @param
     * @return
     */
    @RequestMapping(value = {"/getAllUserMonth"}, method = RequestMethod.GET)
    public List<OnDutyHis> getAllUserMonth(String code) {
        List<OnDutyHis> onDutyHiss = onDutyHisService.getAllUserMonth(code);
        return onDutyHiss;
    }

    /**
     * 发送调班请求
     * @param
     * @return
     */
    @RequestMapping(value = {"/requestTb"}, method = RequestMethod.GET)
    public String requestTb(@RequestParam String id1,@RequestParam String id2) {
        String fl;
        try {
            fl = onDutyHisService.requestTb(id1, id2);
            return fl;
        }catch (Exception e){
            return "failed";
        }
    }

    /**
     * 同意调班请求
     * @param
     * @return
     */
    @RequestMapping(value = {"/arganRequestTb"}, method = RequestMethod.GET)
    public String arganRequestTb(@RequestParam String id1,@RequestParam String id2,@RequestParam String id3) {
        String fl;
        try {
            fl = onDutyHisService.arganRequestTb(id1, id2,id3);
            return fl;
        }catch (Exception e){
            return "failed";
        }
    }

    /**
     * 拒绝调班请求
     * @param
     * @return
     */
    @RequestMapping(value = {"/refuseRequestTb"}, method = RequestMethod.GET)
    public String refuseRequestTb(@RequestParam String id1,@RequestParam String id2,@RequestParam String id3) {
        String fl;
        try {
            fl = onDutyHisService.refuseRequestTb(id1, id2,id3);
            return fl;
        }catch (Exception e){
            return "failed";
        }
    }

    /**
     * 根据主键获取值班信息
     * @param
     * @return
     */
    @RequestMapping(value = {"/getZbById"}, method = RequestMethod.GET)
    public OnDutyHistory getZbById(@RequestParam String id) {
        OnDutyHistory oh=new OnDutyHistory();
        try {
            oh = onDutyHisService.getZbById(id);
            return oh;
        }catch (Exception e){
            return oh;
        }
    }

    /**
     * 查询未值班信息往后30天
     * @param
     * @return
     */
    @RequestMapping(value = {"/getUserMonthBy30"}, method = RequestMethod.GET)
    public List<OnDutyHis> getUserMonthBy30() {
        List<OnDutyHis> onDutyHis = onDutyHisService.getUserMonthBy30();
        return onDutyHis;
    }

    /**
     * 查询个人值班记录
     * @param
     * @return
     */
    @RequestMapping(value = {"/getUserMonthByGr"}, method = RequestMethod.GET)
    public List<OnDutyHis> getUserMonthByGr(@RequestParam String code) {
        List<OnDutyHis> onDutyHis = onDutyHisService.getUserMonthByGr(code);
        return onDutyHis;
    }
}
